const { getHtmlWithPuppeteer } = require('./utils')
const { searchUrl, uniFriendUrl } = require('./utils/enum')
const dayjs = require('dayjs')
const { addTimeGround, parseTimePlace, isAddFn, updateFriend, uploadImg, parseFileID } = require('./utils/method')

const axios = require('axios')

async function main() {
    try {
        let friendListRes = await axios({
            method: 'post',
            data: {
                where: { used: null },
                limit: 100,
            },
            url: uniFriendUrl.get,
        })
        for (let i = 0; i < friendListRes.data.data.length; i++) {
            const { lemmaTitle, lemmaId, coverPic, _id } = friendListRes.data.data[i]
            console.log(lemmaId, lemmaTitle)
            const { coverUrl, baseInfo, posterUrlArr } = await getHtmlWithPuppeteer(
                `${searchUrl}/${lemmaTitle}/${lemmaId}`,
            )
            if (baseInfo) {
                const { birthPlace, birthTime } = parseTimePlace(baseInfo)
                //基本信息中可能没有出生日期与出生地
                if (birthTime && birthPlace) {
                    console.log(birthPlace, dayjs(birthTime).format('YYYY-MM-DD'))
                    const isAdd = isAddFn(birthPlace)
                    if (isAdd) {
                        const groundRes = await addTimeGround({
                            lemmaId,
                            lemmaTitle,
                            birthTime,
                            birthPlace,
                            coverUrl,
                            posterUrlArr,
                            baseInfo,
                        })
                        if (groundRes) {
                            let uniCoverPic
                            if (coverPic) {
                                const upRes = await uploadImg({
                                    lemmaId,
                                    imageUrl: coverPic,
                                })
                                if (upRes.code == 200) {
                                    uniCoverPic = parseFileID(upRes.data.fileID)
                                }
                            }

                            await updateFriend({
                                _id,
                                uniCoverPic,
                                birthPlace,
                                birthTime,
                                used: true,
                            })
                            console.log(lemmaTitle, '添加成功')
                        }
                    } else {
                        const delRes = await axios({
                            method: 'POST',
                            data: {
                                _id,
                            },
                            url: uniFriendUrl.del,
                        })
                        console.log('删除', lemmaTitle)
                    }
                } else {
                    //如果解析不出来出生日期与地点，仅标记为used
                    const upRes = await uploadImg({
                        lemmaId,
                        imageUrl: coverPic,
                    })

                    if (upRes.code == 200) {
                        await updateFriend({
                            _id,
                            uniCoverPic: parseFileID(upRes.data.fileID),
                            birthPlace,
                            birthTime,
                            used: true,
                        })
                        console.log(lemmaTitle, '添加成功')
                    }
                }
            } else {
                console.log('抓取百度数据失败，可能触发了人机验证')
                break
            }

            // addTimeGround({ lemmaId, lemmaTitle,birthTime,birthPlace })
        }
        await main()
    } catch (error) {
        console.error('获取页面内容时出错:', error.message)
        main()
    }
}
// getFriendList()
// 启动主函数
main()
